package com.hamilton.erik.pt.core.util;

import org.mindrot.jbcrypt.*;

import com.hamilton.erik.pt.core.model.User;

public class AuthenticationUtil {

	public static final String PRE_EXISTING_USER = "{\"status\":\"AUTH_FAILURE\",\"reason\":\"USER_NAME_EXISTS\"}";
	public static final String AUTH_FAILURE = "{\"status\":\"AUTH_FAILURE\",\"reason\":\"UNKNOWN\"}";
	public static final String AUTH_SUCCESS = "{\"status\":\"AUTH_SUCCESS\"}";
	
	private static AuthenticationUtil instance = null;
		
	public static AuthenticationUtil getInstance() {
		if(instance == null) {
			instance = new AuthenticationUtil();
		}
		
		return instance;
	}

	private AuthenticationUtil() {}
	
	public String generateSalt() {
		return BCrypt.gensalt();
	}
		
	public String hash(String password, String salt) {
		return BCrypt.hashpw(password, salt);
	}
	
	public boolean authenticateCredentials(User user, String checkPassword) {
		String checkHash = BCrypt.hashpw(checkPassword, user.getSalt());
		
		if(checkHash.equals(user.getHash())) {
			return true;
		}
		
		return false;
	}
}


